package cn.kgc.javaweb.service.impl;

import cn.kgc.javaweb.dao.RoleDao;
import cn.kgc.javaweb.dao.UserDao;
import cn.kgc.javaweb.dao.impl.RoleDaoImpl;
import cn.kgc.javaweb.dao.impl.UserDaoImpl;
import cn.kgc.javaweb.entity.Role;
import cn.kgc.javaweb.entity.User;
import cn.kgc.javaweb.service.RoleService;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: 课工场
 * @Version: v1.0  2023/5/23
 * @Description:
 */
public class RoleServiceImpl implements RoleService {

    private RoleDao roleDao = new RoleDaoImpl();

    private UserDao userDao = new UserDaoImpl();

    @Override
    public List<Role> getList() {

        try {
            return roleDao.getList();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }

    @Override
    public HashMap<String, Object> selectPage(String page, String limit) {


        HashMap<String, Object> map = new HashMap<>();

        int intPage = Integer.parseInt(page);
        int limte = Integer.parseInt(limit);
        Integer start = (intPage-1)*limte;
        // 1.分数数据查询
        List<Role>  roles =  roleDao.getPage(start,limte);
        // 2.数据总条数
        Long count =  roleDao.getTotal();

        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",roles);
        map.put("count",count);

        return map;
    }

    @Override
    public HashMap<String, Object> addRole(String roleName) {

        HashMap<String, Object> map = new HashMap<>();

        try {
            roleDao.insertRole(roleName);
            map.put("code",0);
            map.put("msg","添加成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            map.put("code",500);
            map.put("msg","添加失败");
        }
        return map;
    }

    @Override
    public Role getById(Integer id) {

        return roleDao.getById(id);
    }

    @Override
    public HashMap<String, Object> updateRole(String roleName, Integer id) {

        HashMap<String, Object> map = new HashMap<>();
        try {
            roleDao.updateById(roleName,id);
            map.put("code",0);
            map.put("msg","更新成功");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            map.put("code",500);
            map.put("msg","更新失败");
        }
        return map;
    }

    @Override
    public HashMap<String, Object> deleteById(Integer id) {
        HashMap<String, Object> map = new HashMap<>();
        //1. 判定当前角色是否被占用
          List<User>  users = userDao.selectByRoleId(id);
          if(users.size()>0){
              map.put("code",500);
              map.put("msg","角色被占用,不能删除");
          }else {
              // 2. 根据id删除角色信息
              try {
                  roleDao.deleteById(id);
                  map.put("code",0);
                  map.put("msg","角色删除成功");
              } catch (SQLException throwables) {
                  throwables.printStackTrace();
                  map.put("code",500);
                  map.put("msg","角色删除失败");
              }
          }
        return map;
    }
}
